/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : main.c                                         	      *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-07-8                                             *
 *  $Hard Version     : KF32A156-MINI-EVB_V1.2                                *
 *  $Description$     : Provide the operation interface of data area flash,   *
 * 						including reading, erasing, page writing, byte 		  *
 * 						writing, comparison and other functions		  		  *	
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyright protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-07-08 00.02.00 FAE Group     Version 2.0 update                      *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
**                      		Include Files                                **
******************************************************************************/
#include "system_init.h"
#include "Usart.h"
#include "stdio.h"
#include "Flash.h"
#include "ChipMessageApi.h"

/*******************************************************************************
**                      	Private Macro Definitions                         **
*******************************************************************************/
#define FlashDataSize 				512
#define FlashInformationDataSize	257
/******************************************************************************
**                   	Private variables  Definitions                       **
******************************************************************************/
/* Flash Data buffer */
uint32_t FlashDataBuffer[FlashDataSize];
uint32_t FlashInformationDataBuffer[FlashInformationDataSize];
uint8_t FlashEEDataBuffer[] = {
	0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
	/*******************************************************************************
**                     		   Global Functions 		             	      **
*******************************************************************************/
	/**
 *  @brief :Initializes the GPIO of the Usart
 *  @param in :None
 *  @param out :None
 *  @retval :PF5 -- USART4_TX0
 *
 */
void Usart_Gpio_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	/* Configure PF5 remap mode, as TX pin, it should be pullup */
	/* Configure PF5 remap mode */
	GPIO_InitStructure.m_Mode = GPIO_MODE_RMP;
	GPIO_InitStructure.m_OpenDrain = GPIO_POD_PP;
	GPIO_InitStructure.m_PullDown = GPIO_NOPULL;
	GPIO_InitStructure.m_PullUp = GPIO_PULLUP;
	GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_5;
	GPIO_Configuration(GPIOF_SFR , &GPIO_InitStructure);
	/* Configure PF5 remap function to AF6 */
	GPIO_Pin_RMP_Config(GPIOF_SFR, GPIO_Pin_Num_5, GPIO_RMP_PG1_AF6);
}

/*******************************************************************************
**                     			main Functions 		             	     	  **
*******************************************************************************/
int main()
{
	uint32_t i, FlashRetVal;
	/* Initialize the system clock */
	SystemInit(SYSCLK_FREQ);
	/* Setup SysTick Timer as delay function */
	systick_delay_init(SYSCLK_FREQ);
	/* Initialize the USART IOs */
	Usart_Gpio_Init();
	/* USARTx configured as follow:
		- BaudRate = 115200 baud
		- Word Length = 8 Bits
		- One Stop Bit
		- No parity
		- Hardware flow control disabled (RTS and CTS signals)
		- Receive and transmit enabled
	 */
	USART_Async_config(USART4_SFR);
	/* Get the MCU Unique ID */
	fprintf(USART4_STREAM,"START Flash Read And WRite Example\r\n");
	if (__getChipUniqueSerialNumber(FlashDataBuffer) == CMD_SUCCESS)
	{
		for (i = 0; i < 4; i++)
		{
			fprintf(USART4_STREAM,"MCU UniqueID[%d] is 0x%x \r\n", i, FlashDataBuffer[i]);
		}
	}
	/* Read from 0x20000 address to 'FlashDataSize' data */
	FlashReadNByte(0x20000, FlashDataSize * sizeof(FlashDataBuffer[0]), (uint8_t *)FlashDataBuffer);
	fprintf(USART4_STREAM,"Read address 0x20000\r\n");
	for (i = 0; i < FlashDataSize; i++)
	{
		fprintf(USART4_STREAM,"%d 0x%x\t", i, FlashDataBuffer[i]);
	}
	fprintf(USART4_STREAM,"\r\n\r\n");
	/* Prepare data */
	for (i = 0; i < FlashDataSize; i++)
	{
		FlashDataBuffer[i] = FlashDataBuffer[i] + 1;
	}
	/* First erase and then write. The interrupt is closed inside the function, and then recover */
	/* Write 'FlashDataBuffer' to address 0x20000 about 2K data */
	FlashRetVal = FlashWriteNKBytes(0x20000, 2048, (uint8_t *)FlashDataBuffer);
	if (FlashRetVal == CMD_SUCCESS)
	{
		fprintf(USART4_STREAM,"Write 2 Kbytes success\r\n\r\n");
	}
	/* Write 'FlashDataBuffer' to address 0x30005 about 10 byte */
	FlashRetVal = FlashEEWriteNBytes(0x30005, 10, FlashEEDataBuffer);
	if (FlashRetVal == CMD_SUCCESS)
	{
		fprintf(USART4_STREAM,"Write Byte success\r\n\r\n");
	}

	/* Prepare data */
	for (i = 0; i < FlashInformationDataSize; i++)
	{
		FlashInformationDataBuffer[i] = i;
	}
	/* Write 'FlashDataBuffer' to Flash date area (address 0x1C00) about 1K data */
	fprintf(USART4_STREAM,"Write Flash date area ,address 0x1C00\r\n");
	FlashRetVal = FlashCFGUserWrite(0x1C00, 1024, FlashInformationDataBuffer);
	if (FlashRetVal == CMD_SUCCESS)
	{
		fprintf(USART4_STREAM,"Write 1Kbytes success\r\n\r\n");
	}
	/* Read from Flash date area (address 0x1C00)  to 'FlashDataBuffer' data */
	fprintf(USART4_STREAM,"Read Flash date area ,address 0x1C00\r\n");
	FlashRetVal = FlashCFGUserRead(0x1C00, 1024, FlashInformationDataBuffer);
	for (i = 0; i < 257; i++)
	{
		fprintf(USART4_STREAM,"%d 0x%x\t", i, FlashInformationDataBuffer[i]);
	}
	fprintf(USART4_STREAM,"\r\n\r\n");

	while (1)
	{

	}
}

/**
 *  @brief : Reports the name of the source file and the source line number
 *           where the assert_param error has occurred.
 *  @param in :	file pointer to the source file name
 * 	@param in :	line assert_param error line source number
 *  @param out :None
 *  @retval :None
 */
void check_failed(uint8_t *File, uint32_t Line)
{
	/* User can add his own implementation to report the file name and line number,
		ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
		;
	}
};
